package net.njcp.mybatis.frame.builder;

import net.njcp.mybatis.frame.utils.MapUtil;
import net.njcp.mybatis.frame.utils.MatchesUtil;
import net.njcp.mybatis.frame.utils.QueryUtil;
import net.njcp.mybatis.frame.utils.ReflectUtil;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @ClassName UpdateSqlBuilder
 * @Description: 针对单个对象修改操作
 * @Author 柳拓
 * @Date 2019/11/6
 * @Version V1.0
 **/
public class UpdateSqlBuilder {

    public static SQL buildUpdateSql(Object object){
        return new SQL() {{
            Class entityClass = object.getClass();
            String tableName = QueryUtil.getTableName(entityClass);
            //表名称构造
            StringBuilder tableBuilder = new StringBuilder();
            tableBuilder.append(tableName);
            UPDATE(tableBuilder.toString());
            try {
                set(this, entityClass, MapUtil.Obj2Map(object));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }};
    }

    public static void set(SQL sql, Class entityClass, Map<String, Object> map){
        String primaryKey = ReflectUtil.getPrimaryKey(entityClass);
        map.entrySet().stream().filter(e->e.getValue()!=null).forEach(
                e -> {
                    String fieldName = MatchesUtil.switchParam(e.getKey());
                    if (e.getKey().equals(primaryKey)){
                        if (!(e.getValue() instanceof Comparable)){
                            try {
                                MapUtil.Obj2Map(e.getValue()).entrySet().stream().filter(f->f.getValue()!=null).forEach(
                                        (f)->{
                                            sql.WHERE(MatchesUtil.switchParam(f.getKey())+" = #{"+e.getKey()+"."+f.getKey()+"}");
                                        }
                                );
                            } catch (Exception ex) {
                                throw new RuntimeException(ex.getMessage());
                            }
                        }else{
                            sql.WHERE(fieldName+" = #{"+e.getKey()+"}");
                        }
                    }else {
                        //判断如果属性是对象类型，将对象转为Map
                        if (!(e.getValue() instanceof Comparable)){
                            try {
                                MapUtil.Obj2Map(e.getValue()).entrySet().stream().filter(f->f.getValue()!=null).forEach(
                                        (f)->{
                                            sql.SET(MatchesUtil.switchParam(f.getKey())+" = #{"+e.getKey()+"."+f.getKey()+"}");
                                        }
                                );
                            } catch (Exception ex) {
                                throw new RuntimeException(ex.getMessage());
                            }
                        }else{
                            sql.SET(fieldName+" = #{"+e.getKey()+"}");
                        }
                    }
                }
        );
    }

}
